package group.program.Controller;

import group.program.Service.RegisterService;
import group.program.pojo.trafficPojo.Result;
import group.program.pojo.trafficPojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RegisterController {
    @Autowired
    private RegisterService registerService;

    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        System.out.println(user.toString());
        String validationError = validateUserName(user.getUser_name());
        if (validationError != null) {
            return Result.error(validationError);
        }

        validationError = validatePassword(user.getPassword());
        if (validationError != null) {
            return Result.error(validationError);
        }

        validationError = validateFullName(user.getFull_name());
        if (validationError != null) {
            return Result.error(validationError);
        }

        validationError = validatePhoneNumber(user.getPhone_number());
        if (validationError != null) {
            return Result.error(validationError);
        }

        validationError = validateEmail(user.getEmail());
        if (validationError != null) {
            return Result.error(validationError);
        }

        int isSuccess = registerService.register(user);
        if (isSuccess!=0) {
            return Result.success("注册成功");
        } else {
            return Result.error("注册失败");
        }
    }

    // UserName Validation
    public String validateUserName(String userName) {
        String lengthError = validateUserNameLength(userName);
        if (lengthError != null) return lengthError;

        String formatError = validateUserNameFormat(userName);
        if (formatError != null) return formatError;

        return checkIfUserNameExists(userName);
    }

    public String validateUserNameLength(String userName) {
        if (userName.length() < 4 || userName.length() > 10) {
            return "用户名长度必须大于等于4位，小于10位";
        }
        return null;
    }

    public String validateUserNameFormat(String userName) {
        if (!userName.matches("[a-zA-Z]+")) {
            return "用户名只能由英文字母组成";
        }
        return null;
    }

    public String checkIfUserNameExists(String userName) {
        if (registerService.isUserNameExist(userName)!=null) {
            return "用户名已存在";
        }
        return null;
    }


    // Password Validation
    public String validatePassword(String password) {
        String lengthError = validatePasswordLength(password);
        if (lengthError != null) return lengthError;

        String formatError = validatePasswordFormat(password);
        if (formatError != null) return formatError;

        return null;
    }

    public String validatePasswordLength(String password) {
        if (password.length() < 5 || password.length() >= 12) {
            return "密码长度必须大于等于5位，小于12位";
        }
        return null;
    }

    public String validatePasswordFormat(String password) {
        if (!password.matches("[a-zA-Z0-9]+")) {
            return "密码必须由数字或者英文字母组成";
        }
        return null;
    }

    // Full Name Validation
    public String validateFullName(String fullName) {
        String formatError = validateFullNameFormat(fullName);
        if (formatError != null) return formatError;

        return checkIfFullNameExists(fullName);
    }

    public String validateFullNameFormat(String fullName) {
        if (!fullName.matches("[\\u4e00-\\u9fa5]+")) {
            return "真实姓名只能由中文组成";
        }
        return null;
    }

    public String checkIfFullNameExists(String fullName) {
        if (registerService.isFullNameExist(fullName)!=null) {
            return "真实姓名已存在";
        }
        return null;
    }

    // Phone Number Validation
    public String validatePhoneNumber(String phoneNumber) {
        String formatError = validatePhoneNumberFormat(phoneNumber);
        if (formatError != null) return formatError;

        return checkIfPhoneNumberExists(phoneNumber);
    }

    public String validatePhoneNumberFormat(String phoneNumber) {
        if (!phoneNumber.matches("\\d{11}")) {
            return "电话号码必须是11位的数字";
        }
        return null;
    }

    public String checkIfPhoneNumberExists(String phoneNumber) {
        if (registerService.isPhoneNumberExist(phoneNumber)!=null) {
            return "电话号码已存在";
        }
        return null;
    }

    // Email Validation
    public String validateEmail(String email) {
        String formatError = validateEmailFormat(email);
        if (formatError != null) return formatError;
        return checkIfEmailExists(email);
    }

    public String validateEmailFormat(String email) {
        if (!email.matches("^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$")) {
            return "邮件地址格式不正确";
        }
        return null;
    }

    public String checkIfEmailExists(String email) {
        if (registerService.isEmailExist(email)!=null) {
            return "邮件地址已存在";
        }
        return null;
    }
}
